<template>
  <wd-notice-bar
    v-if="!useUserStore().isLogined"
    text="关注公众号一诺滚雪球获取体验账号"
    prefix="sound"
    type="warning"
    custom-class="space"
  />
  <div class="user flex mt-8 ml-8">
    <image
      class="rounded-50% w-80px h-80px"
      :src="useUserStore().userInfo.avatar"
    />
    <div class="ml-4 mt-4 text-lg font-bold" @click="goLogin">
      {{ useUserStore().userInfo.name }}
    </div>
  </div>
  <div class="mt-8 px-4">
    <wd-cell
      v-if="useUserStore().isLogined"
      title="退出登录"
      is-link
      @click="logout"
    />
    <wd-cell
      v-if="useUserStore().isLogined"
      title="更新用户信息"
      is-link
      @click="getUserInfo"
    />
  </div>
  <wd-toast />
  <official-account class="fixed bottom-0 left-0 right-0" />
</template>

<script setup lang="ts">
import { getUserInfoById } from '@/api/user';
import { ResultCodeEnum } from '@/enums';
import { useUserStore } from '@/store';
import { useToast } from 'wot-design-uni';

const goLogin = () => {
  uni.navigateTo({
    url: useUserStore().isLogined
      ? '/pages/center/register/index?type=edit'
      : '/pages/center/login/index'
  });
};

const logout = () => {
  useUserStore().clearUserInfo();
};

const { success } = useToast();

const getUserInfo = async () => {
  const res = await getUserInfoById(useUserStore().userInfo.id);
  if (res.code === ResultCodeEnum.SUCCESS) {
    success({
      msg: '刷新用户信息成功'
    });
    useUserStore().setUserInfo({
      ...res.data,
      accessToken: useUserStore().userInfo.accessToken,
      refreshToken: useUserStore().userInfo.refreshToken
    });
  }
};

onShareAppMessage(() => ({
  title: '个人中心',
  path: '/pages/center/index'
}));
</script>

<style lang="scss" scoped></style>
